/*
 * RBFUpload.h
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 */

#ifndef RBFUPLOAD_H_
#define RBFUPLOAD_H_
#include "RBFCommon.h"
#define __packed __attribute__((__packed__))

/*
 * cmd = 0x04  节点主动事件上报
 * payload: MsgSeq(1B) + MsgRetry(1B) + BattV(1B) + RSSI(1B) + Temp(1B)
 *          + MacAdd(2B) + QrfR1(1B) + QrfR2(1B) + EchASeq(1B) + KeyValue(nB)
 *
 */
typedef struct
{
  uint16_t macAdd;  //[15]
  uint8_t msgSeq;   //[17]
  uint8_t msgRetry; //[18]
  uint8_t battV;    //[19]
  uint8_t rssi;     //[20]
  uint8_t temp;     //[21]
  uint8_t qrfR1;    //[22]
  uint8_t qrfR2;    //[23]
  uint8_t echASeq;  //[24]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageNodeUploadEvent_t;

typedef enum
{
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_MACADD = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_MSGSEQ = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_MSGRETRY = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_BATTV = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_RSSI = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 5,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_TEMP = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 6,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_QRFR1 = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 7,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_QRFR2 = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 8,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_ECHASEQ = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 9,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_KEYVALUE = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_FIRST + 10,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_LAST = RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_KEYVALUE,
} RBFMAC_PAYLOAD_NODE_UPLOAD_EVENT_INDEX_E;

#define RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_LEN (sizeof(RBFMacMessageNodeUploadEvent_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_NODE_UPLOAD_EVENT_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_LEN)
#else
#define RBF_MAC_PACKET_NODE_UPLOAD_EVENT_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_UPLOAD_EVENT_LEN)
#endif

/*
 * cmd = 0x05  节点心跳上报  需包含自身外设输入状态
 * payload: MsgSeq(1B) + MsgRetry(1B) + BattV(1B) + RSSI(1B) + Temp(1B)
 *          + MacAdd(2B) + QrfR1(1B) + QrfR2(1B) + EchASeq(1B) + KeyValue(nB)
 *
 */
typedef struct
{
  uint16_t macAdd;  //[15]
  uint8_t msgSeq;   //[17]
  uint8_t msgRetry; //[18]
  uint8_t battV;    //[19]
  uint8_t rssi;     //[20]
  uint8_t temp;     //[21]
  uint8_t qrfR1;    //[22]
  uint8_t qrfR2;    //[23]
  uint8_t echASeq;  //[24]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageNodeUploadHeartbeat_t;

typedef enum
{
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_MACADD = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_MSGSEQ = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_MSGRETRY = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_BATTV = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_RSSI = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 5,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_TEMP = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 6,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_QRFR1 = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 7,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_QRFR2 = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 8,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_ECHASEQ = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_FIRST + 9,
  RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_LAST = RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_ECHASEQ,
} RBFMAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_INDEX_E;

#define RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_LEN (sizeof(RBFMacMessageNodeUploadHeartbeat_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_UPLOAD_HEARTBEAT_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_LEN)
#else
#define RBF_MAC_PACKET_UPLOAD_HEARTBEAT_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_NODE_UPLOAD_HEARTBEAT_LEN)
#endif

/*
 * CMD = 0x06  网关应答上报
 * payload: RSSI(1B) + QrfH1(1B) + QrfH2(1B) + ASeq(1B) + KeyValue(nB)
 *
 * GATEWAY_ACK_EVENT
 */
typedef struct
{
  uint8_t rssi;  //[16]
  uint8_t qrfH1; //[17]
  uint8_t qrfH2; //[18]
  uint8_t aseq;  //[19]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageGWAckEvent_t;

typedef enum
{
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_RSSI = RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_QRFH1 = RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST + 1,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_QRFH2 = RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_ASEQ = RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_KEYVALUE = RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_GW_ACK_EVENT_INDEX_LAST = RBF_MAC_PAYLOAD_GW_ACK_EVENT_KEYVALUE,
} RBFMAC_PAYLOAD_GW_ACK_EVENT_INDEX_E;

#define RBF_MAC_PAYLOAD_GW_ACK_EVENT_LEN (sizeof(RBFMacMessageGWAckEvent_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GW_ACK_EVENT_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_ACK_EVENT_LEN)
#else
#define RBF_MAC_PACKET_GW_ACK_EVENT_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_ACK_EVENT_LEN)
#endif

/*
 * CMD = 0x07  网关应答心跳
 * payload: RSSI(1B) + QrfH1(1B) + QrfH2(1B) + ASeq(1B) + KeyValue(nB)
 *
 * GATEWAY_ACK_HEARTBEAT
 */
typedef struct
{
  uint8_t rssi;  //[16]
  uint8_t qrfH1; //[17]
  uint8_t qrfH2; //[18]
  uint8_t aseq;  //[19]
  uint8_t keyValueSize;
  uint8_t *keyValue;
} __packed RBFMacMessageGWAckHeartbeat_t;

typedef enum
{
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST = RBF_MAC_HEADER_INDEX_LAST + 1,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_RSSI = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_QRFH1 = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST + 1,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_QRFH2 = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST + 2,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_ASEQ = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST + 3,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_KEYVALUE = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_FIRST + 4,
  RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_LAST = RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_KEYVALUE,
} RBFMAC_PAYLOAD_GW_ACK_HEARTBEAT_INDEX_E;

#define RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_LEN (sizeof(RBFMacMessageGWAckHeartbeat_t) - sizeof(uint8_t) - sizeof(uint8_t *))

#if defined(PACKET_0_INCLUDE)
#define RBF_MAC_PACKET_GW_ACK_HEARTBEAT_LEN (RBF_MAC_PACKET_LEN_SIZE + RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_LEN)
#else
#define RBF_MAC_PACKET_GW_ACK_HEARTBEAT_LEN (RBF_HEADER_LENGTH_MAX + RBF_MAC_PAYLOAD_GW_ACK_HEARTBEAT_LEN)
#endif

/*
 * 节点实时事件上报payload
 */
// 解包
void RBF_node_upload_event_payload_packetToMsg(uint8_t *packet, RBFMacMessageNodeUploadEvent_t *msg);
// 组包
void RBF_node_upload_event_payload_msgToPacket(RBFMacMessageNodeUploadEvent_t msg, uint8_t *packet);

/*
 * 节点心跳上报payload
 */
// 解包
void RBF_node_upload_heart_payload_packetToMsg(uint8_t *packet, RBFMacMessageNodeUploadHeartbeat_t *msg);
// 组包
void RBF_node_upload_heart_payload_msgToPacket(RBFMacMessageNodeUploadHeartbeat_t msg, uint8_t *packet);

/*
 * 网关应答上报payload解包
 */
// 解包
void RBF_gw_ack_event_payload_packetToMsg(uint8_t *packet, RBFMacMessageGWAckEvent_t *msg);
// 组包
void RBF_gw_ack_event_payload_msgToPacket(RBFMacMessageGWAckEvent_t msg, uint8_t *packet);

/*
 * 网关应答心跳payload解包
 */
// 解包
void RBF_gw_ack_heart_payload_packetToMsg(uint8_t *packet, RBFMacMessageGWAckHeartbeat_t *msg);
// 组包
void RBF_gw_ack_heart_payload_msgToPacket(RBFMacMessageGWAckHeartbeat_t msg, uint8_t *packet);




#endif /* RBFUPLOAD_H_ */
